﻿using OrderSystem.Common;
using OrderSystem.Templet;
using System;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryPayment : FormTemplet
    {
        private int _nCustomerID = -1;

        public FormQueryPayment()
        {
            this.InitializeComponent();
        }

        private void FormQueryOrders_Load(object sender, EventArgs e)
        {
            this.dtpPaymentDateStart.Enabled = false;
            this.dtpPaymentDateEnd.Enabled = false;
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomerName.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
            }
        }

        private void chkPaymentDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkPaymentDateUsed.Checked)
            {
                this.dtpPaymentDateStart.Enabled = true;
                this.dtpPaymentDateEnd.Enabled = true;
            }
            else
            {
                this.dtpPaymentDateStart.Enabled = false;
                this.dtpPaymentDateEnd.Enabled = false;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            PaymentCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormPaymentList1 formPaymentList = new FormPaymentList1(conditionForQuery);
                formPaymentList.MdiParent = base.MdiParent;
                formPaymentList.Show();
                formPaymentList.Activate();
            }
        }

        private PaymentCondition GetConditionForQuery()
        {
            PaymentCondition paymentCondition = new PaymentCondition();
            PaymentCondition result;
            if (this.txtCustomerName.Text.Trim() == "" && !this.chkPaymentDateUsed.Checked && this.txtPaymentProject.Text.Trim() == "" && this.cbWayOfPayment.Text.Trim() == "" && this.txtPaymentAddress.Text.Trim() == "" && this.txtRemark.Text.Trim() == "")
            {
                MessageBox.Show("至少要输入一条查询信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                result = null;
            }
            else
            {
                if (this.txtCustomerName.Text.Trim() != "")
                {
                    if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID == -1)
                    {
                        MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtCustomerName.Focus();
                        result = null;
                        return result;
                    }
                    paymentCondition.CustomerID = this._nCustomerID;
                }
                if (this.chkPaymentDateUsed.Checked)
                {
                    int num = this.dtpPaymentDateStart.Value.CompareTo(this.dtpPaymentDateEnd.Value);
                    if (num > 0)
                    {
                        MessageBox.Show("订购日期：后面的日期要大于或等于前面的日期", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpPaymentDateStart.Focus();
                        result = null;
                        return result;
                    }
                    paymentCondition.PaymentDateStart = this.dtpPaymentDateStart.Value.ToShortDateString() + " 00:00:00";
                    paymentCondition.PaymentDateEnd = this.dtpPaymentDateEnd.Value.ToShortDateString() + " 23:59:59";
                }
                if (this.txtPaymentProject.Text.Trim() != "")
                {
                    if (this.txtPaymentProject.Text.Trim().Length > 32)
                    {
                        MessageBox.Show("款项 字符数量不能大于 32", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtPaymentAddress.Focus();
                        result = null;
                        return result;
                    }
                    paymentCondition.PaymentProject = this.txtPaymentProject.Text.Trim();
                }
                if (this.cbWayOfPayment.Text.Trim() != "")
                {
                    if (this.cbWayOfPayment.Text.Trim().Length > 16)
                    {
                        MessageBox.Show("付款方式 字符数量不能大于 16", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.cbWayOfPayment.Focus();
                        result = null;
                        return result;
                    }
                    paymentCondition.WayOfPayment = this.cbWayOfPayment.Text.Trim();
                }
                if (this.txtPaymentAddress.Text.Trim() != "")
                {
                    if (this.txtPaymentAddress.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("地址 字符数量不能大于 64", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtPaymentAddress.Focus();
                        result = null;
                        return result;
                    }
                    paymentCondition.PaymentAddress = this.txtPaymentAddress.Text.Trim();
                }
                if (this.txtRemark.Text.Trim() != "")
                {
                    if (this.txtRemark.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("备注 字符数量不能大于64", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtRemark.Focus();
                        result = null;
                        return result;
                    }
                    paymentCondition.Remark = this.txtRemark.Text.Trim();
                }
                if (this.ck已结算.Checked || this.ck被结算.Checked || this.ck未结算.Checked || this.ck作废.Checked)
                {
                    string text = "";
                    if (this.ck已结算.Checked)
                    {
                        text = "'S'";
                    }
                    if (this.ck被结算.Checked)
                    {
                        if (text == "")
                        {
                            text = "'P'";
                        }
                        else
                        {
                            text += ",'P'";
                        }
                    }
                    if (this.ck未结算.Checked)
                    {
                        if (text == "")
                        {
                            text = "'N'";
                        }
                        else
                        {
                            text += ",'N'";
                        }
                    }
                    if (this.ck作废.Checked)
                    {
                        if (text == "")
                        {
                            text = "'C'";
                        }
                        else
                        {
                            text += ",'C'";
                        }
                    }
                    paymentCondition.SettleMark = text;
                }
                result = paymentCondition;
            }
            return result;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
